package tw.com.book.task02;

import org.apache.commons.lang3.StringUtils;

import tw.com.book.R;
import tw.com.book.UserActivity;
import tw.com.book.api.task03.Task03Service;
import tw.com.book.componet.Deglagte;
import tw.com.book.componet.IssueBussinessException;
import tw.com.book.componet.Msg;
import tw.com.book.core.task03.Task03ServiceImpl;
import tw.com.book.dto.Task003DTO;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

/**
 * 註冊控制器
 * 
 * @author Andy
 * 
 */
public class SignAccountActivity extends Activity implements Deglagte {

	private transient EditText email;
	private transient EditText password;
	private Task003DTO dto = null;
	private Task03Service task03Service;

	private Button sumit;
	private Button clean;

	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.sign_user_view);

		final Bundle bundle = this.getIntent().getExtras();

		this.dto = (Task003DTO) bundle.getSerializable("dto");

		this.email = (EditText) this.findViewById(R.id.email);
		this.password = (EditText) this.findViewById(R.id.password);

		this.sumit = (Button) this.findViewById(R.id.sumit);
		this.sumit.setOnClickListener(new SumitAction());

		this.clean = (Button) this.findViewById(R.id.clean);
		this.clean.setOnClickListener(new CleanAction());

		this.task03Service = new Task03ServiceImpl();

	}

	private class SumitAction implements OnClickListener {

		@Override
		public void onClick(View v) {
			try {
				signAccount();
			} catch (IssueBussinessException e) {
				Toast.makeText(SignAccountActivity.this, e.toString(),
						Toast.LENGTH_SHORT).show();
				;
			}

		}

	}

	private class CleanAction implements OnClickListener {

		@Override
		public void onClick(View v) {
			email.setText("");
			password.setText("");
		}

	}

	public void signAccount() {
		if (StringUtils.isBlank(this.email.getText().toString())) {
			throw new IssueBussinessException(//
					Msg.W001,//
					getString(R.string.email));//
		}
		if (StringUtils.isBlank(this.password.getText().toString())) {
			throw new IssueBussinessException(//
					Msg.W001,//
					getString(R.string.password));//
		}

		this.dto.setEmail(this.email.getText().toString());
		this.dto.setPassowrd(this.password.getText().toString());

		this.task03Service.signAccount(dto, this);

	}

	@Override
	public void putMessage(String messsage, IssueBussinessException e) {
		if (StringUtils.equals(messsage, Task03Service.LOGGING_ACTION)) {

		
		}
		if (StringUtils.equals(messsage, Task03Service.SIGN_ACTION)) {
			if (e == null) {
				this.finish();
			} else {
				
				Toast.makeText(this, "註冊失敗", Toast.LENGTH_SHORT).show();
			}
		}

	}
}
